﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.BLL;
using Maticsoft.Model;

namespace FineUI.Examples
{
    public partial class student_query_share : System.Web.UI.Page
    {
        //下载
        protected void DocDownLoad(string current_RS_Id, string current_T_Id, string current_S_Id)
        {

            Maticsoft.BLL.ReportSubmit documentation = new Maticsoft.BLL.ReportSubmit();
            Maticsoft.Model.ReportSubmit documentInform = documentation.GetModel(current_RS_Id, current_T_Id, current_S_Id);
            string fileRpath = Server.MapPath(@"~/document/student/") + documentInform.RS_Path;

            Response.ClearHeaders();
            Response.Clear();
            Response.Expires = 0;
            Response.Buffer = true;
            Response.AddHeader("Accept-Language", "zh-tw");
            string name = documentInform.RS_Formerly.ToString();
            System.IO.FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] byteFile = new byte[files.Length];

            files.Read(byteFile, 0, byteFile.Length);
            files.Close();
            Response.ContentType = "application/octet-stream;charset=gbk";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));

            Response.BinaryWrite(byteFile);
            Response.Flush();
            Response.End();


        }
        protected DataSet GetTeaShareInfrom()
        {
            Maticsoft.BLL.ReportSubmit RS = new Maticsoft.BLL.ReportSubmit();
            DataSet currentDS = RS.GetShareInfromation("Student.S_Id=ReportSubmit.S_Id and ReportSubmit.T_Id=Teacher.T_Id and ReportSubmit.RS_Share='已共享'");

            DataView DSShareInfrom = currentDS.Tables[0].DefaultView;
            Grid1.DataSource = DSShareInfrom;
            Grid1.DataBind();

            return currentDS;
        
        
        }
        protected void ExistsPermiss(string P_Id, string P_Status)
        {
            Maticsoft.BLL.Permissions PM = new Maticsoft.BLL.Permissions();
            if (!PM.ExistsPermiss(P_Id, P_Status))
            {
                Response.Redirect("~/user_no_permission.aspx");
            }

        }

         
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }
            ExistsPermiss("S007", "已授权");
            if(!IsPostBack)
            {
                GetTeaShareInfrom();
            }

        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            GetTeaShareInfrom();
        }
//预览
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionDownload")
            {
                int index = e.RowIndex;
                DataSet current_DS = GetTeaShareInfrom();
                string currentS_Id = current_DS.Tables[0].Rows[index]["S_Id"].ToString();
                string currentT_Id = current_DS.Tables[0].Rows[index]["T_Id"].ToString();
                string currentRS_Id = current_DS.Tables[0].Rows[index]["RS_Id"].ToString();
                DocDownLoad(currentRS_Id, currentT_Id, currentS_Id);

            }
            if (e.CommandName == "ActionPreview")
            {
                int index = e.RowIndex;
                DataSet current_DS = GetTeaShareInfrom();
                string currentS_Id = current_DS.Tables[0].Rows[index]["S_Id"].ToString();
                string currentT_Id = current_DS.Tables[0].Rows[index]["T_Id"].ToString();
                string currentRS_Id = current_DS.Tables[0].Rows[index]["RS_Id"].ToString();

                Maticsoft.BLL.ReportSubmit documentation = new Maticsoft.BLL.ReportSubmit();
                Maticsoft.Model.ReportSubmit documentInform = documentation.GetModel(currentRS_Id, currentT_Id, currentS_Id);
                string fileRpath = Server.MapPath(@"~/document/student/") + documentInform.RS_Path;

                Session["Doc_Path"] = fileRpath;
                Session["Doc_Flag"] = "student";
                Response.Redirect("~/user_view_document.aspx");
            
            }


        }

        /*protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionDownLoad")
            {
                int index = e.RowIndex;
                DataSet current_DS = GetTeaShareInfrom();
                string currentS_Id = Session["UserName"].ToString();
                string currentT_Id = current_DS.Tables[0].Rows[index]["T_Id"].ToString();
                string currentRS_Id = current_DS.Tables[0].Rows[index]["RS_Id"].ToString();
                DocDownLoad(currentRS_Id, currentT_Id, currentS_Id);

            }
        }*/
    }
}